package com.dianwang.utils;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public  class EntityRecognizer {

    /**
     * 正则包含:
     *  1) 有电压等级 + "xxx(变电站|站)"
     *  2) 或者纯 "xxx变电站"
     *  3) 或者纯 "xxx站"
     *
     * 举例能匹配：
     *  "500kV济南变变电站"、
     *  "500KV聊城站"、
     *  "滕州变电站" 等等
     */
    private static final Pattern SUBSTATION_PATTERN = Pattern.compile(
            "(\\d{1,4}kV\\s*[\\u4e00-\\u9fa5A-Za-z0-9]+(变电站|站)|[\\u4e00-\\u9fa5A-Za-z0-9]+变电站|[\\u4e00-\\u9fa5A-Za-z0-9]+站)",
            Pattern.CASE_INSENSITIVE
    );

    public static class Entities {
        String substation;
        String equipment;
        String defectContent;
        public Entities(String substation, String equipment, String defectContent) {
            this.substation = substation;
            this.equipment = equipment;
            this.defectContent = defectContent;
        }
    }

    public static Entities recognizeEntities(String input) {
        // 1) 识别变电站
        String substation = detectSubstation(input);

        // 2) 识别设备
        String equipment = null;
        Map<String, String> equipDict = StandardizationDictionaries.equipmentDict;
        for (String key : equipDict.keySet()) {
            if (input.contains(key)) {
                equipment = equipDict.get(key);
                break;
            }
        }

        // 3) 缺陷内容(简单示例: 去掉设备名称后的剩余文本)
        String defectContent;
        if (equipment != null) {
            int idx = input.indexOf(equipment);
            if (idx != -1) {
                defectContent = input.substring(idx + equipment.length()).trim();
            } else {
                defectContent = input;
            }
        } else {
            defectContent = input;
        }

        return new Entities(substation, equipment, defectContent);
    }

    /**
     * 核心变电站识别方法
     * - 通过正则捕捉 "500kVxxx变电站" 或 "xxx站"
     * - 然后做重复字修正 (fixRepeats)
     * - 若只是以"站"结尾，就补上"变电站"
     */
    private static String detectSubstation(String text) {
        Matcher m = SUBSTATION_PATTERN.matcher(text);
        if (m.find()) {

            String raw = m.group(0);
            // 修正"变变电站"等重复字
            raw = fixRepeats(raw);

            // 如果只是以“站”结尾，但不是“变电站”，就替换成“变电站”
            //         比如匹配到 "济南站" => "济南变电站"
            if (raw.endsWith("站") && !raw.endsWith("变电站")) {
                raw = raw.replaceAll("站$", "变电站");
            }

            // 最后把"500KV"修正成"500kV" (正则里是CASE_INSENSITIVE，但最终输出最好统一小写kV)
            raw = raw.replaceAll("KV", "kV");
            raw = raw.replaceAll("^.*?(\\d{1,4}kV\\s*[\\u4e00-\\u9fa5A-Za-z0-9]+变电站).*", "$1");
            raw = processRawText(raw);
            return raw;
        }
        return "未识别变电站";
    }

    public static String processRawText(String rawText) {
        if (rawText == null || rawText.isEmpty()) {
            return "";
        }


        String[] keywords = {"对", "在","于","的","是","和","有"};
        String processedText = rawText;

        for (String keyword : keywords) {
            int index = processedText.indexOf(keyword);
            if (index != -1 && index + 1 < processedText.length()) {
                processedText = processedText.substring(index + 1);
            }
        }

        return processedText;
    }

    /**
     * 修正字符串中的一些重复字样，例如:
     *   "变变电站" => "变电站"
     *   (可按需扩展更多规则)
     */
    private static String fixRepeats(String input) {
        // 简单示例：把所有"变变电站"替换成"变电站"
        // 如果有更多类似重复，可以在这里写多行replaceAll

        while (input.contains("变变电站")) {
            input = input.replaceAll("变变电站", "变电站");
        }

        return input;
    }
}